package me.leiusual.sharing.ui.shop

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations.switchMap
import androidx.lifecycle.ViewModel
import me.leiusual.sharing.api.model.BusinessApply
import me.leiusual.sharing.api.model.businesscondition.BusinessCondition
import me.leiusual.sharing.pref.Store
import me.leiusual.sharing.repository.BusinessRepository
import me.leiusual.sharing.repository.model.Data
import me.leiusual.sharing.util.extension.toLiveData
import javax.inject.Inject

/**
 * author: leiusual
 * create on: 2019-07-30
 * description:
 */
class ShopBusinessApplyViewModel @Inject constructor(
    private val businessRepository: BusinessRepository,
    val store: Store
) : ViewModel() {

  // 条件列表

  private val _businessCondition = MutableLiveData<ParamsBusiness>()
  val businessCondition: LiveData<Data<BusinessCondition>> = switchMap(_businessCondition) {
    businessRepository.businessApply(it.type, it.level).toLiveData()
  }

  fun businessCondition(type: String, level: String) {
    _businessCondition.value = ParamsBusiness(type, level)
  }

  data class ParamsBusiness(val type: String, val level: String)

  // 提交申请

  private val _businessApply = MutableLiveData<BusinessApply.Request>()
  val businessApply: LiveData<Data<List<BusinessApply.Response>>> = switchMap(_businessApply) {
    businessRepository.businessApply(it).toLiveData()
  }

  fun businessApply(request: BusinessApply.Request) {
    _businessApply.value = request
  }
}